package com.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.model.Book;
import com.web.model.Category;
import com.web.model.Product;
import com.web.service.ProductService;
import com.web.service.ServiceDAOFactory;

@WebServlet("/main.do")
public class MainServlet extends HttpServlet {

	private static final long serialVersionUID = 4203324038743582945L;
	
	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		doPost(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		String method = req.getParameter("method");
		Map<String,List> datas = new HashMap<String,List>();
		if(method !=null){
			switch(method){
			case "1":
				productDetail(req,resp);
				break;
			default:
				break;
			}
		}else{
			//Step1 分类浏览
			initCategory(datas);
			
			//Step2 选择推荐
			initRecommend(datas);
			
			//Step3 热销图书
			initHotBooks(datas);
			
			//Step4     
			initNewBooks(datas);
			
			req.setAttribute("datas", datas);
			req.getRequestDispatcher("/WEB-INF/view/home/main.jsp").forward(req, resp);
		}
	}
	
	
//	private void productDetail(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//		Book book =null;
//		
//	}
	
	//Step1 分类浏览
	private void initCategory(Map<String,List>  datas){
		List<Category> cats;
		try{
			List<Category> list = ServiceDAOFactory.getCategoryService().findAll();
			cats = findByParentId(list,1);
			//为子目录subCats赋值
			for(Category c : cats){
				c.setSubCats(findByParentId(list,c.getId()));
			}
			datas.put("category", cats);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	// Stept 2  推荐图书
	private void initRecommend(Map<String,List> datas){
		List<Book> productList = new ArrayList<Book>();
		ProductService productService = ServiceDAOFactory.getProductService();
		try{
			productList.add(productService.findById((int)(Math.random() * 20) + 1));
			productList.add(productService.findById((int)(Math.random() * 20) + 1));
			
			datas.put("books", productList);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	//Stept 3  热销图书
	private void initHotBooks(Map<String,List> datas){
		List<Book> books = new ArrayList<Book>();
		List<Product> pros;
		try{
			pros = ServiceDAOFactory.getProductService().findByHot(4);
			for(Product p : pros){
				books.add((Book) p);
			}
			datas.put("hotBooks", books);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//Stept4 新书上店
	private void initNewBooks(Map<String , List> datas){
		List<Book> books = new ArrayList<Book>();
		List<Product> pros;
		try{
			pros = ServiceDAOFactory.getProductService().findByNew(4);
			for(Product p : pros){
				books.add((Book) p);
			}
			datas.put("newBooks", books);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	
	//根据父目录id获取子目录
	private List<Category> findByParentId(List<Category> list,int id){
		List<Category> subCats = new ArrayList<Category>();
		for(Category c :list){
			if(c.getParentId() == id){
				subCats.add(c);
			}
		}
		return subCats;
	}
	
	
	private void productDetail(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		Book book = null;
		try{
			String id= req.getParameter("id");
			if(id!=null){
				book = (Book) ServiceDAOFactory.getProductService().findById(new Integer(id));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		req.setAttribute("book", book);
		req.getRequestDispatcher("/WEB-INF/view/home/product.jsp").forward(req, resp);
	}
	
	
}
